package com.qidian.mes.service.impl;

import com.qidian.mes.mapper.ProcessMapper;
import com.qidian.mes.mapper.RouteProcessMapper;
import com.qidian.mes.pojo.Process;
import com.qidian.mes.pojo.RouteProcess;
import com.qidian.mes.service.RouteProcessService;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class RouteProcessServiceImpl implements RouteProcessService {
    @Autowired
    private RouteProcessMapper routeProcessMapper;
    @Autowired
    private ProcessMapper processMapper;
    @Override
    public MESResult list(Long routeId) {
        return MESResult.ok(routeProcessMapper.list(routeId));
    }

    @Override
    public MESResult save(RouteProcess routeProcess) {
        Process process=processMapper.getByProcessName(routeProcess.getProcessName());
        routeProcess.setProcessId(process.getProcessId());
        routeProcess.setProcessCode(process.getProcessCode());
        routeProcessMapper.save(routeProcess);
        return MESResult.ok();
    }

    @Override
    public MESResult delete(Long recordId) {
        routeProcessMapper.deleteByRecordId(recordId);
        return MESResult.ok();
    }

    @Override
    public MESResult get(Long recordId) {
        RouteProcess routeProcess =null;
        if ((routeProcess=routeProcessMapper.selectByRecordId(recordId)) !=null){
            return MESResult.ok(routeProcess);
        }
        return MESResult.empty();
    }

    @Override
    public MESResult update(RouteProcess routeProcess) {
        Process process=processMapper.getByProcessName(routeProcess.getProcessName());
        routeProcess.setProcessId(process.getProcessId());
        routeProcess.setProcessCode(process.getProcessCode());
        routeProcessMapper.update(routeProcess);
        return MESResult.ok();
    }
}
